package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private JedisPool jedisPool;

    @Override
    public Result add(CheckGroup checkGroup) {
        checkGroupMapper.save(checkGroup);
        addCheckGroupAndCheckItem(checkGroup);
        return Result.success(MessageConstant.ADD_CHECKGROUP_SUCCESS, null);
    }

    @Override
    public Result pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.selectByCondition(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return Result.success(MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    @Override
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        List<CheckItem> items = checkItemMapper.findAll();
        List<Integer> itemIds = checkGroupMapper.findCheckItemIdsByGroupId(id);
        checkGroup.setCheckItems(items);
        checkGroup.setCheckItemIds(itemIds);
        return Result.success(MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }

    @Override
    public Result update(CheckGroup checkGroup) {
        checkGroupMapper.update(checkGroup);
        checkGroupMapper.deleteCheckItemIdsByCheckGroupId(checkGroup.getId());
        addCheckGroupAndCheckItem(checkGroup);
        return Result.success(MessageConstant.EDIT_CHECKGROUP_SUCCESS, null);
    }

    @Override
    public Result deleteById(Integer id) {
        Integer count = checkGroupMapper.findSetmealByGroupId(id);
        if (count > 0) {
            return Result.fail("当前检查组被引用，不能删除");
        }
        checkGroupMapper.deleteById(id);
        return Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS, null);
    }

    @Override
    public Result findAll() {
        Jedis jedis = jedisPool.getResource();
        String checkGroupAll = jedis.get("checkGroupAll");
        List<CheckGroup> list;
        try {
            if (StringUtil.isEmpty(checkGroupAll)) {
                System.out.println("从数据库获取数据");
                list = checkGroupMapper.findAll();
                jedis.setex("checkGroupAll", 60, JSON.toJSONString(list));
            }else {
                System.out.println("从redis获取数据");
                list = JSON.parseArray(checkGroupAll,CheckGroup.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(MessageConstant.QUERY_CHECKGROUP_FAIL);
        } finally {
            if (jedis!=null){
                jedis.close();
            }
        }
        return Result.success(MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    private void addCheckGroupAndCheckItem(CheckGroup checkGroup) {
        if (checkGroup.getCheckItemIds() != null || checkGroup.getCheckItemIds().size() > 0) {
            for (Integer checkItemId : checkGroup.getCheckItemIds()) {
                checkGroupMapper.saveCheckGroupAndItem(checkGroup.getId(), checkItemId);
            }
        }
    }
}
